using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using MEA.AE_Data;

namespace MEA.AE_BusinessLogic
{
	public class UIEventFilter : Form
	{
		public enum ESetEvFilter
		{
			eSetA,
			eSetB,
			eSetVal,
			eSetTextBoxIDs,
			eSetAll,
			eSetShowEventsTimeInValid
		}

		private enum ESeekSubstr
		{
			eNotFound,
			eIlegal,
			eSeekId,
			eSeekStr
		}

		private GroupBox groupBox1;

		private int myWidth;

		private int myHeight;

		private DataGridView dataGridViewFilter;

		private Label selectionsLabel;

		private TextBox filterTextBox;

		public bool setVal;

		public ESetEvFilter fIlterType;

		public string filterValue;

		public int rowx;

		private Button allButton;

		private bool allButtonVal;

		public bool eventFilterShowed;

		private CheckBox chkBoxTimeValidShow;

		private EventFilterData filterEventFilterData;

		private List<char> hexList = new List<char>
		{
			'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
			'a', 'b', 'c', 'd', 'e', 'f', 'A', 'B', 'C', 'D',
			'E', 'F'
		};

		private IContainer components;

		private CheckBox checkBoxShowECUA;

		private CheckBox checkBoxShowECUB;

		public event ItemCheckEventHandler ev_EVFilterSet;

		public UIEventFilter(ref EventFilterData data)
		{
			groupBox1 = new GroupBox();
			dataGridViewFilter = new DataGridView();
			selectionsLabel = new Label();
			filterTextBox = new TextBox();
			filterEventFilterData = data;
			eventFilterShowed = true;
		}

		public void setUIEventFilter(ref EventFilterData listOfevents)
		{
			new List<EventLogEntry>();
			InitializeComponent();
			CenterToParent();
			filterEventFilterData = listOfevents;
			myWidth = base.Width;
			myHeight = base.Height;
			allButton = new Button();
			allButton.Location = new Point(136, 8);
			allButton.Name = "allButton";
			allButton.Size = new Size(154, 23);
			allButton.TabIndex = 3;
			allButton.Text = "Deselect all Events";
			allButton.UseVisualStyleBackColor = true;
			allButton.Click += b_AllButton;
			allButtonVal = true;
			chkBoxTimeValidShow = new CheckBox();
			chkBoxTimeValidShow.Name = "chkBoxTimeValidShow";
			chkBoxTimeValidShow.Text = "Show invalid time stamps";
			chkBoxTimeValidShow.AutoSize = true;
			chkBoxTimeValidShow.Checked = false;
			chkBoxTimeValidShow.CheckState = CheckState.Unchecked;
			chkBoxTimeValidShow.Location = new Point(136, 35);
			chkBoxTimeValidShow.Size = new Size(88, 17);
			chkBoxTimeValidShow.UseVisualStyleBackColor = true;
			chkBoxTimeValidShow.CheckStateChanged += b_CheckBoxShowAllEv;
			base.Controls.Add(chkBoxTimeValidShow);
			ToolTip toolTip = new ToolTip();
			toolTip.AutoPopDelay = 7000;
			toolTip.InitialDelay = 1000;
			toolTip.ReshowDelay = 500;
			toolTip.ShowAlways = true;
			toolTip.SetToolTip(chkBoxTimeValidShow, "If this Check Box is checked,\nevents with invalid time stamps are also displayed\notherwise only events with valid time stamps\nDefault: not checked");
			Size size = new Size(myWidth - 42, myHeight - 112);
			groupBox1.AutoSizeMode = AutoSizeMode.GrowAndShrink;
			groupBox1.Name = "groupBox1";
			groupBox1.TabIndex = 2;
			groupBox1.TabStop = false;
			groupBox1.Text = "Selection";
			groupBox1.Size = size;
			groupBox1.Controls.Add(dataGridViewFilter);
			groupBox1.Controls.Add(selectionsLabel);
			groupBox1.Controls.Add(filterTextBox);
			groupBox1.Location = new Point(12, 62);
			groupBox1.AutoSizeMode = AutoSizeMode.GrowAndShrink;
			DataGridViewCheckBoxColumn dataGridViewCheckBoxColumn = new DataGridViewCheckBoxColumn(false);
			dataGridViewCheckBoxColumn.Frozen = true;
			dataGridViewCheckBoxColumn.HeaderText = "active";
			dataGridViewCheckBoxColumn.Name = "dataGridViewFixTextActiv";
			dataGridViewCheckBoxColumn.ReadOnly = false;
			dataGridViewCheckBoxColumn.Resizable = DataGridViewTriState.False;
			dataGridViewCheckBoxColumn.Width = 32;
			dataGridViewCheckBoxColumn.TrueValue = CheckState.Checked;
			dataGridViewCheckBoxColumn.FalseValue = CheckState.Unchecked;
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
			dataGridViewTextBoxColumn.Frozen = true;
			dataGridViewTextBoxColumn.HeaderText = "DTC";
			dataGridViewTextBoxColumn.MinimumWidth = 32;
			dataGridViewTextBoxColumn.Name = "dataGridViewFixTextDTC";
			dataGridViewTextBoxColumn.ReadOnly = true;
			dataGridViewTextBoxColumn.Resizable = DataGridViewTriState.False;
			dataGridViewTextBoxColumn.SortMode = DataGridViewColumnSortMode.Automatic;
			dataGridViewTextBoxColumn.Width = 35;
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn2 = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn2.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
			dataGridViewTextBoxColumn2.HeaderText = "Description";
			dataGridViewTextBoxColumn2.Frozen = true;
			dataGridViewTextBoxColumn2.MinimumWidth = 200;
			dataGridViewTextBoxColumn2.Name = "dataGridViewFixTextDTCDes";
			dataGridViewTextBoxColumn2.ReadOnly = true;
			dataGridViewTextBoxColumn2.Resizable = DataGridViewTriState.True;
			dataGridViewTextBoxColumn2.SortMode = DataGridViewColumnSortMode.NotSortable;
			dataGridViewTextBoxColumn2.Width = 600;
			selectionsLabel.AutoSize = true;
			selectionsLabel.Location = new Point(16, groupBox1.Height - 32);
			selectionsLabel.Name = "selectionsLabel";
			selectionsLabel.Size = new Size(72, 13);
			selectionsLabel.TabIndex = 21;
			selectionsLabel.Text = "Filter String:";
			filterTextBox.Enabled = true;
			filterTextBox.Font = new Font("Tahoma", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
			filterTextBox.Location = new Point(86, groupBox1.Height - 36);
			filterTextBox.Name = "filterTextBox";
			filterTextBox.Size = new Size(151, 21);
			filterTextBox.Text = "";
			filterTextBox.Leave += FilterTextBoxLeave;
			filterTextBox.KeyDown += FilterTextBoxEnter;
			ToolTip toolTip2 = new ToolTip();
			toolTip2.AutoPopDelay = 7000;
			toolTip2.InitialDelay = 1000;
			toolTip2.ReshowDelay = 500;
			toolTip2.ShowAlways = true;
			toolTip2.SetToolTip(filterTextBox, "For submission press Enter or Tab.\nFor connecting of more terms use '+' (OR connection)\nSelection terms e.g.:\n- 1d3\n  shows events which DTC started with this term\n- rail\n  shows events witch Description contain this term");
			dataGridViewFilter.AllowUserToAddRows = false;
			dataGridViewFilter.AllowUserToDeleteRows = false;
			dataGridViewFilter.AllowUserToResizeRows = false;
			dataGridViewFilter.BackgroundColor = SystemColors.Window;
			dataGridViewFilter.BorderStyle = BorderStyle.Fixed3D;
			dataGridViewFilter.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			dataGridViewFilter.Columns.AddRange(dataGridViewCheckBoxColumn, dataGridViewTextBoxColumn, dataGridViewTextBoxColumn2);
			dataGridViewFilter.GridColor = SystemColors.ActiveBorder;
			dataGridViewFilter.Location = new Point(16, 28);
			dataGridViewFilter.MultiSelect = false;
			dataGridViewFilter.Name = "dataGridViewFilter";
			dataGridViewFilter.ReadOnly = true;
			dataGridViewFilter.RowHeadersVisible = false;
			dataGridViewFilter.RowTemplate.ReadOnly = true;
			dataGridViewFilter.RowTemplate.Resizable = DataGridViewTriState.False;
			dataGridViewFilter.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
			dataGridViewFilter.Width = groupBox1.Width - 32;
			dataGridViewFilter.Height = groupBox1.Height - 28 - 16 - 26;
			dataGridViewFilter.TabIndex = 40;
			if (listOfevents.listOfShowedEventsID.Count > 0)
			{
				dataGridViewFilter.Rows.Clear();
				for (int i = 0; i < listOfevents.listOfShowedEventsID.Count; i++)
				{
					EventLogEntry eventLogEntry = listOfevents.filterListOfEvents[i];
					if (eventLogEntry != null)
					{
						CheckState checkState = (listOfevents.listOfShowedEvents[i] ? CheckState.Checked : CheckState.Unchecked);
						object[] values = new object[3]
						{
							checkState,
							string.Format("{0:X4}", eventLogEntry.eventID),
							eventLogEntry.description
						};
						dataGridViewFilter.Rows.Add(values);
					}
				}
				dataGridViewFilter.AutoResizeColumns();
			}
			checkBoxShowECUA.Checked = listOfevents.showECUAEvents;
			checkBoxShowECUB.Checked = listOfevents.showECUBEvents;
			groupBox1.Controls.Add(dataGridViewFilter);
			base.Controls.Add(groupBox1);
			base.Controls.Add(allButton);
			groupBox1.ResumeLayout(false);
			((ISupportInitialize)dataGridViewFilter).EndInit();
			ResumeLayout(false);
			PerformLayout();
			EnableDisableFilterMenu(true);
		}

		public void EnableDisableFilterMenu(bool setx)
		{
			if (setx)
			{
				dataGridViewFilter.MouseClick += DataGridMouseClick;
			}
			else
			{
				dataGridViewFilter.MouseClick -= DataGridMouseClick;
			}
			dataGridViewFilter.Enabled = setx;
			allButton.Enabled = setx;
			groupBox1.Enabled = setx;
			checkBoxShowECUA.Enabled = setx;
			checkBoxShowECUB.Enabled = setx;
			chkBoxTimeValidShow.Enabled = setx;
		}

		private void DataGridMouseClick(object sender, MouseEventArgs e)
		{
			if (e.Button != MouseButtons.Left)
			{
				return;
			}
			fIlterType = ESetEvFilter.eSetVal;
			DataGridView dataGridView = (DataGridView)sender;
			rowx = dataGridView.CurrentRow.Index;
			if ((CheckState)dataGridView.CurrentRow.Cells[0].Value == CheckState.Checked)
			{
				if (filterEventFilterData.listOfShowedEvents[rowx])
				{
					filterEventFilterData.listOfShowedEvents[rowx] = false;
					dataGridView.CurrentRow.Cells[0].Value = CheckState.Unchecked;
					if (this.ev_EVFilterSet != null)
					{
						this.ev_EVFilterSet(this, null);
					}
				}
			}
			else if ((CheckState)dataGridView.CurrentRow.Cells[0].Value == CheckState.Unchecked && !filterEventFilterData.listOfShowedEvents[rowx])
			{
				filterEventFilterData.listOfShowedEvents[rowx] = true;
				dataGridView.CurrentRow.Cells[0].Value = CheckState.Checked;
				if (this.ev_EVFilterSet != null)
				{
					this.ev_EVFilterSet(this, null);
				}
			}
		}

		public void addEvUIEventFilterl()
		{
			dataGridViewFilter.MouseClick += DataGridMouseClick;
		}

		private void b_UIEventFilterResizeEnd(object sender, EventArgs e)
		{
			myWidth = base.Width;
			myHeight = base.Height;
			groupBox1.Width = myWidth - 42;
			groupBox1.Height = myHeight - 112;
			dataGridViewFilter.Width = myWidth - 74;
			dataGridViewFilter.Height = myHeight - 182;
			selectionsLabel.Location = new Point(16, myHeight - 144);
			filterTextBox.Location = new Point(86, myHeight - 148);
		}

		private void b_CheckBoxShowAllEv(object sender, EventArgs e)
		{
			CheckBox checkBox = (CheckBox)sender;
			filterEventFilterData.showEventsTimeInvalid = checkBox.Checked;
			setVal = checkBox.Checked;
			fIlterType = ESetEvFilter.eSetShowEventsTimeInValid;
			chkBoxTimeValidShow.Text = "Show Ev. only with valid Tm.St.";
			if (this.ev_EVFilterSet != null)
			{
				this.ev_EVFilterSet(this, null);
			}
		}

		private void b_CheckBoxECUAChanged(object sender, EventArgs e)
		{
			filterEventFilterData.showECUAEvents = checkBoxShowECUA.Checked;
			setVal = checkBoxShowECUA.Checked;
			fIlterType = ESetEvFilter.eSetA;
			if (this.ev_EVFilterSet != null)
			{
				this.ev_EVFilterSet(this, null);
			}
		}

		private void b_CheckBoxECUBChanged(object sender, EventArgs e)
		{
			setVal = checkBoxShowECUB.Checked;
			fIlterType = ESetEvFilter.eSetB;
			if (this.ev_EVFilterSet != null)
			{
				this.ev_EVFilterSet(this, null);
			}
		}

		private void b_AllButton(object sender, EventArgs e)
		{
			fIlterType = ESetEvFilter.eSetAll;
			if (allButtonVal)
			{
				allButtonVal = false;
				allButton.Text = "Select all Events";
				setVal = false;
				for (int i = 0; i < filterEventFilterData.listOfShowedEvents.Count; i++)
				{
					filterEventFilterData.listOfShowedEvents[i] = setVal;
					dataGridViewFilter.Rows[i].Cells[0].Value = CheckState.Unchecked;
				}
			}
			else
			{
				allButtonVal = true;
				allButton.Text = "Deselect all Events";
				setVal = true;
				for (int j = 0; j < filterEventFilterData.listOfShowedEvents.Count; j++)
				{
					filterEventFilterData.listOfShowedEvents[j] = setVal;
					dataGridViewFilter.Rows[j].Cells[0].Value = CheckState.Checked;
				}
			}
			if (this.ev_EVFilterSet != null)
			{
				this.ev_EVFilterSet(this, null);
			}
		}

		private void FilterTextBoxEnter(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Return)
			{
				RunFilterTextBox();
			}
		}

		private void FilterTextBoxLeave(object sender, EventArgs e)
		{
			RunFilterTextBox();
		}

		private void RunFilterTextBox()
		{
			bool flag = true;
			fIlterType = ESetEvFilter.eSetTextBoxIDs;
			filterValue = filterTextBox.Text;
			string[] array = filterValue.Split('+');
			for (int i = 0; i < array.Length; i++)
			{
				array[i] = array[i].Trim();
			}
			for (int i = 0; i < array.Length; i++)
			{
				if (!(array[i] != string.Empty))
				{
					continue;
				}
				switch (testIfIDFilter(array[i]))
				{
				case ESeekSubstr.eSeekId:
				{
					int num = Convert.ToInt32(array[i], 16);
					int num2 = 4 - array[i].Length;
					num <<= num2 * 4;
					int num3;
					switch (num2)
					{
					case 3:
						num3 = num | 0xFFF;
						break;
					case 2:
						num3 = num | 0xFF;
						break;
					case 1:
						num3 = num | 0xF;
						break;
					default:
						num3 = num;
						break;
					}
					for (int k = 0; k < filterEventFilterData.listOfShowedEvents.Count; k++)
					{
						if (filterEventFilterData.listOfShowedEventsID[k] <= num3 && filterEventFilterData.listOfShowedEventsID[k] >= num)
						{
							filterEventFilterData.listOfShowedEvents[k] = true;
							dataGridViewFilter.Rows[k].Cells[0].Value = CheckState.Checked;
						}
						else if (i == 0)
						{
							filterEventFilterData.listOfShowedEvents[k] = false;
							dataGridViewFilter.Rows[k].Cells[0].Value = CheckState.Unchecked;
						}
					}
					break;
				}
				case ESeekSubstr.eSeekStr:
				{
					for (int j = 0; j < filterEventFilterData.listOfShowedEvents.Count; j++)
					{
						if (filterEventFilterData.filterListOfEvents[j].description.Contains(array[i]))
						{
							filterEventFilterData.listOfShowedEvents[j] = true;
							dataGridViewFilter.Rows[j].Cells[0].Value = CheckState.Checked;
						}
						else if (i == 0)
						{
							filterEventFilterData.listOfShowedEvents[j] = false;
							dataGridViewFilter.Rows[j].Cells[0].Value = CheckState.Unchecked;
						}
					}
					break;
				}
				default:
					MessageBox.Show("Search Term: \"" + array[i] + "\" is not legal", "Filter Message");
					flag = false;
					break;
				}
			}
			if (flag && this.ev_EVFilterSet != null)
			{
				this.ev_EVFilterSet(this, null);
			}
		}

		private ESeekSubstr testIfIDFilter(string testStr)
		{
			ESeekSubstr eSeekSubstr = ESeekSubstr.eNotFound;
			if (testStr.Length == 0)
			{
				eSeekSubstr = ESeekSubstr.eIlegal;
			}
			else if (testStr.Length <= 4)
			{
				for (int i = 0; i < testStr.Length; i++)
				{
					if (eSeekSubstr != ESeekSubstr.eNotFound)
					{
						break;
					}
					if (!hexList.Contains(testStr[i]) && i < 4)
					{
						eSeekSubstr = ESeekSubstr.eSeekStr;
					}
				}
				if (eSeekSubstr == ESeekSubstr.eNotFound)
				{
					eSeekSubstr = ESeekSubstr.eSeekId;
				}
			}
			else
			{
				eSeekSubstr = ESeekSubstr.eSeekStr;
			}
			return eSeekSubstr;
		}

		private void b_EventFilterClosed(object sender, FormClosingEventArgs e)
		{
			eventFilterShowed = false;
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing && components != null)
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}

		private void InitializeComponent()
		{
			System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MEA.AE_BusinessLogic.UIEventFilter));
			this.checkBoxShowECUA = new System.Windows.Forms.CheckBox();
			this.checkBoxShowECUB = new System.Windows.Forms.CheckBox();
			base.SuspendLayout();
			this.checkBoxShowECUA.AutoSize = true;
			this.checkBoxShowECUA.Checked = true;
			this.checkBoxShowECUA.CheckState = System.Windows.Forms.CheckState.Checked;
			this.checkBoxShowECUA.Location = new System.Drawing.Point(12, 12);
			this.checkBoxShowECUA.Name = "checkBoxShowECUA";
			this.checkBoxShowECUA.Size = new System.Drawing.Size(88, 17);
			this.checkBoxShowECUA.TabIndex = 0;
			this.checkBoxShowECUA.Text = "Show ECU-A";
			this.checkBoxShowECUA.UseVisualStyleBackColor = true;
			this.checkBoxShowECUA.CheckStateChanged += new System.EventHandler(b_CheckBoxECUAChanged);
			this.checkBoxShowECUB.AutoSize = true;
			this.checkBoxShowECUB.Checked = true;
			this.checkBoxShowECUB.CheckState = System.Windows.Forms.CheckState.Checked;
			this.checkBoxShowECUB.Location = new System.Drawing.Point(12, 35);
			this.checkBoxShowECUB.Name = "checkBoxShowECUB";
			this.checkBoxShowECUB.Size = new System.Drawing.Size(88, 17);
			this.checkBoxShowECUB.TabIndex = 1;
			this.checkBoxShowECUB.Text = "Show ECU-B";
			this.checkBoxShowECUB.UseVisualStyleBackColor = true;
			this.checkBoxShowECUB.CheckStateChanged += new System.EventHandler(b_CheckBoxECUBChanged);
			base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 13f);
			base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			base.ClientSize = new System.Drawing.Size(314, 433);
			base.Controls.Add(this.checkBoxShowECUB);
			base.Controls.Add(this.checkBoxShowECUA);
			base.Icon = (System.Drawing.Icon)resources.GetObject("$this.Icon");
			this.MinimumSize = new System.Drawing.Size(330, 471);
			base.Name = "UIEventFilter";
			this.Text = "Events Filter";
			base.FormClosing += new System.Windows.Forms.FormClosingEventHandler(b_EventFilterClosed);
			base.ResizeEnd += new System.EventHandler(b_UIEventFilterResizeEnd);
			base.ResumeLayout(false);
			base.PerformLayout();
		}
	}
}
